<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>确认订单</p>
    </header>
    <!-- 返回按钮 -->
    <div class="back-button" @click="goBack">
      <i class="fa fa-arrow-left"></i>
      <!-- 使用 Font Awesome 图标 -->
    </div>

    <!-- 收货信息部分 -->
    <div class="order-info">
      <h5>订单配送至：</h5>
      <div v-if="defaultAddress">
        <div class="order-info-address" @click="userAddress">
          <p>{{ defaultAddress.address }}</p>
          <i class="fa fa-angle-right"></i>
        </div>
        <p>{{ defaultAddress.contactName }} {{ defaultAddress.contactPhone }}</p>
      </div>
      <div v-else>
        <div class="order-info-address" @click="userAddress">
          <h2 style="color:white">请选择收货地址</h2>
          <i class="fa fa-angle-right"></i>
        </div>
      </div>
    </div>

    <h3>{{ business.businessName }}</h3>

    <!-- 购物车明细部分 -->
    <ul class="order-detailed">
      <li v-for="item in listCartDetails" :key="item.cartId">
        <div class="order-detailed-left">
          <img :src="item.food.foodImg" />
          <p>¥{{ item.food.foodPrice }}</p>
          <p>{{ item.food.foodName }} x {{ item.quantity }}</p>
        </div>
        <!-- 会员 -->
        <p v-if="user.vip == 1">&#165;{{ getVipPrice(item.quantity * item.food.foodPrice) }}</p>
        <p v-else>&#165;{{ item.quantity * item.food.foodPrice }}</p>
      </li>
    </ul>
    <div class="order-deliveryfee">
      <p>配送费</p>
      <p>&#165;{{ business.deliveryPrice }}</p>
    </div>

    <!-- 合计部分 -->
    <div class="total">
      <div class="total-left">&#165;{{ getTotal }}</div>
      <div class="total-right" @click="toPay">去支付</div>
    </div>
  </div>
</template>

<script>

import * as myConstants from '../const.js'
import { showDialog } from 'vant';

export default {
  data() {
    return {
      discount:myConstants.discount,
      businessId: this.$route.query.businessId,
      business: "", //商家信息
      user: JSON.parse(sessionStorage.getItem("user")), //当前登录的用户信息
      listCartDetails: [], //购物车详情列表
      defaultAddress: "" //默认收货地址
    };
  },
  computed: {
    //计算总价
    getTotal() {
      let total = 0;

      if (this.user.vip == 1) {
        for (let i = 0; i < this.listCartDetails.length; i++) {
          total +=
            this.listCartDetails[i].quantity *
            this.listCartDetails[i].food.foodPrice * this.discount;
        }
      }
      else {
        for (let i = 0; i < this.listCartDetails.length; i++) {
          total +=
            this.listCartDetails[i].quantity *
            this.listCartDetails[i].food.foodPrice;
        }
      }
      return total + this.business.deliveryPrice;
    }
  },
  mounted() {
    this.getBusinessInfo();
    this.getListCartDetails();
    this.getDefaultAddress();
    console.log(this.user);
  },
  methods: {

    getVipPrice(price){
      let VipPrice = price * this.discount
      return VipPrice.toFixed(2)
    },
    // 返回上一页
    goBack() {
      this.$router.go(-1);
    },
    //获取收货地址
    getDefaultAddress() {
      this.defaultAddress = JSON.parse(
        sessionStorage.getItem("defaultAddress")
      );
      // console.log(this.defaultAddress);
    },
    //API创建订单
    toPay() {
      //判断用户是否选择默认收货地址
      if (this.defaultAddress == "" || this.defaultAddress == null || this.defaultAddress == undefined || this.defaultAddress == 'undefined') {
        showDialog({
          message: '请先选择收货地址',
        })
      } else {
        this.$http.get("/orders/createOrders", {
          params: {
            userId: this.user.userId,
            businessId: this.businessId,
            addressId: this.defaultAddress.addressId,
            total: this.getTotal
          }
        })
          .then(res => {
            if (res.data.flag) {
              this.$router.push({path:"/pay",query:{orderId:res.data.object}});
            }
          })
      }

    },
    userAddress() {
      this.$router.push({ path: "/userAddress", query: { businessId: this.businessId } });
    },
    //根据商家编号查询商家信息
    getBusinessInfo() {
      this.$http
        .get("/business/businessInfoById", {
          params: {
            id: this.businessId
          }
        })
        .then(res => {
          // console.log(res)
          this.business = res.data.object;
        });
    },
    //获取购物车详情(包括购物车中的食品数据)
    getListCartDetails() {
      this.$http
        .get("/cart/listCartDetails", {
          params: {
            businessId: this.businessId,
            userId: this.user.userId
          }
        })
        .then(res => {
          // console.log(res)
          this.listCartDetails = res.data.object;
        });
    }
  }
};
</script>

<style scoped>
/****************** 总容器 ******************/
.wrapper {
  width: 100%;
  height: 100%;
}

/****************** header部分 ******************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  color: #fff;
  font-size: 4.8vw;

  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;

  display: flex;
  justify-content: center;
  align-items: center;
}

/****************** 订单信息部分 ******************/
.wrapper .order-info {
  /*注意这里，不设置高，靠内容撑开。因为地址有可能折行*/
  width: 100%;
  margin-top: 12vw;
  background-color: #0097ef;
  box-sizing: border-box;
  padding: 2vw;
  color: #fff;
}

.wrapper .order-info h5 {
  font-size: 3vw;
  font-weight: 300;
}

.wrapper .order-info .order-info-address {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  font-weight: 700;
  user-select: none;
  cursor: pointer;
  margin: 1vw 0;
}

.wrapper .order-info .order-info-address p {
  width: 90%;
  font-size: 5vw;
}

.wrapper .order-info .order-info-address i {
  font-size: 6vw;
}

.wrapper .order-info p {
  font-size: 3vw;
}

.wrapper h3 {
  box-sizing: border-box;
  padding: 3vw;
  font-size: 4vw;
  color: #666;
  border-bottom: solid 1px #ddd;
}

/****************** 订单明细部分 ******************/
.wrapper .order-detailed {
  width: 100%;
}

.wrapper .order-detailed li {
  width: 100%;
  height: 16vw;
  box-sizing: border-box;
  padding: 3vw;
  color: #666;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .order-detailed li .order-detailed-left {
  display: flex;
  align-items: center;
}

.wrapper .order-detailed li .order-detailed-left img {
  width: 10vw;
  height: 10vw;
}

.wrapper .order-detailed li .order-detailed-left p {
  font-size: 3.5vw;
  margin-left: 3vw;
}

.wrapper .order-detailed li p {
  font-size: 3.5vw;
}

.wrapper .order-deliveryfee {
  width: 100%;
  height: 16vw;
  box-sizing: border-box;
  padding: 3vw;
  color: #666;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 3.5vw;
}

/****************** 订单合计部分 ******************/
.wrapper .total {
  width: 100%;
  height: 14vw;

  position: fixed;
  left: 0;
  bottom: 14vw;

  display: flex;
}

.wrapper .total .total-left {
  flex: 2;
  background-color: #505051;
  color: #fff;
  font-size: 4.5vw;
  font-weight: 700;
  user-select: none;

  display: flex;
  justify-content: center;
  align-items: center;
}

.wrapper .total .total-right {
  flex: 1;
  background-color: #38ca73;
  color: #fff;
  font-size: 4.5vw;
  font-weight: 700;
  user-select: none;
  cursor: pointer;

  display: flex;
  justify-content: center;
  align-items: center;
}

.back-button {
  position: fixed;
  top: 5vw;
  /* 根据需要调整高度 */
  left: 4vw;
  /* 给左边留出一定间距 */
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #ffffff;
  font-size: 4vw;
  z-index: 1000;
}
</style>